from django.db.models import F
from apps.system.activityPerformance.dao import ActivityPerformanceDao
from apps.system.user.dao import UserDao

# 更新团队成员营业额
def updateTeamPerformance(scene, user, n, money, activity=None):
    # print('更新', scene, user.id, n, money)
    if user and user.id:
        # 更新团队总人数
        _user = UserDao().findOneByObjId(user.id)
        if activity:
            _activityPerformance = None
            _activityPerformances = ActivityPerformanceDao().findAll().filter(activity=activity, user=_user)
            if _activityPerformances.exists():
                _activityPerformance = _activityPerformances.first()
            else:
                _activityPerformance = ActivityPerformanceDao().create({'activity': activity.id, 'user': _user.id})

            if scene == 'add':
                _activityPerformance.teamOrderNumber = F('teamOrderNumber') + n
                _activityPerformance.teamPerformance = F('teamPerformance') + money
                _activityPerformance.save()
            elif scene == 'sub':
                if _activityPerformance.teamOrderNumber >= n:
                    _activityPerformance.teamOrderNumber = F('teamOrderNumber') - n
                    _activityPerformance.save()
                else:
                    _activityPerformance.teamOrderNumber = 0
                    _activityPerformance.save()

                if _user.teamPerformance >= money:
                    _user.teamPerformance = F('teamPerformance') - money
                    _user.save()
                else:
                    _user.teamPerformance = 0
                    _user.save()

        else:
            if scene == 'add':
                _user.teamOrderNumber = F('teamOrderNumber') + n
                _user.teamPerformance = F('teamPerformance') + money
                _user.save()
            elif scene == 'sub':
                if _user.teamOrderNumber >= n:
                    _user.teamOrderNumber = F('teamOrderNumber') - n
                    _user.save()
                else:
                    _user.teamOrderNumber = 0
                    _user.save()

                if _user.teamPerformance >= money:
                    _user.teamPerformance = F('teamPerformance') - money
                    _user.save()
                else:
                    _user.teamPerformance = 0
                    _user.save()

        if _user.parent:
            updateTeamPerformance(scene, _user.parent, n, money, activity)
    else:
        return False


# 更新团队人数
def updateTeamPeopleNumber(scene, user, n, activity):
    # print('更新', scene, user.id, n)
    if user and user.id:
        # 更新团队总人数
        _user = UserDao().findOneByObjId(user.id)
        if scene == 'add':
            _user.teamPeopleNumber = F('teamPeopleNumber') + n
            _user.save()
        elif scene == 'sub':
            if _user.teamPeopleNumber >= n:
                _user.teamPeopleNumber = F('teamPeopleNumber') - n
                _user.save()
            else:
                _user.teamPeopleNumber = 0
                _user.save()

        if _user.parent:
            updateTeamPeopleNumber(scene, _user.parent, n)
    else:
        return False


# 更新团队订单数
def updateTeamOrderNumber(scene, user, n):
    print('更新', scene, user.id, n)
    if user and user.id:
        # 更新团队总人数
        _user = UserDao().findOneByObjId(user.id)
        if scene == 'add':
            _user.teamOrderNumber = F('teamOrderNumber') + n
            _user.save()
        elif scene == 'sub':
            if _user.teamOrderNumber >= n:
                _user.teamOrderNumber = F('teamOrderNumber') - n
                _user.save()
            else:
                _user.teamOrderNumber = 0
                _user.save()

        if _user.parent:
            updateTeamOrderNumber(scene, _user.parent, n)
    else:
        return False